﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using IncrementalBackups.Properties;
using System.Windows.Forms;

namespace IncrementalBackups.Helpers
{
    public static class LogsHelper
    {

        /// <summary>
        /// Writes to log.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <param name="message">The message.</param>
        /// <param name="sendMail">if set to <c>true</c> [send mail].</param>
        public static void WriteToErrorLog(Exception ex, string message, bool sendMail)
        {
            System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
            try
            {
                string logLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), Resources.ErrorLogFileName);
                if (!File.Exists(logLocation))
                    File.WriteAllText(logLocation, message + Environment.NewLine + ex.ToString());
                else
                {
                    using (StreamWriter sw = File.AppendText(logLocation))
                    {
                        sw.WriteLine();
                        sw.WriteLine(message + " " + DateTime.Now.ToString());
                        sw.WriteLine(ex);

                        if (trace.FrameCount > 0)
                        {
                            sw.WriteLine("Stack Trace info:");
                            sw.WriteLine(trace.GetFrame(0).GetMethod().Name);
                            sw.WriteLine("Line: " + trace.GetFrame(0).GetFileLineNumber());
                            sw.WriteLine("Column: " + trace.GetFrame(0).GetFileColumnNumber());
                        }
                    }
                }
            }
            catch (Exception exlog)
            { }
            //Send mail 
            //if (sendMail)
            //{
            //    try
            //    {
            //        //Send a message with one line of code 
            //        // RC.Gmail.GmailMessage.SendFromGmail("username", "password", "toAddress@gmail.com", "subject", "message body");

            //        //Send a message with one line of code with a MailMessage object 
            //        //RC.Gmail.GmailMessage.SendMailMessageFromGmail("username", "password", mailMessageObject);

            //        //Use the GmailMessage object to create and send your message 
            //        StringBuilder sb = new StringBuilder();
            //        sb.AppendLine(message + " " + DateTime.Now.ToString());
            //        sb.AppendLine(ex.ToString());
            //        if (trace.FrameCount > 0)
            //        {
            //            sb.AppendLine("Stack Trace info:");
            //            sb.AppendLine(trace.GetFrame(0).GetMethod().Name);
            //            sb.AppendLine("Line: " + trace.GetFrame(0).GetFileLineNumber());
            //            sb.AppendLine("Column: " + trace.GetFrame(0).GetFileColumnNumber());
            //        }
            //        RC.Gmail.GmailMessage gmailMsg = new RC.Gmail.GmailMessage("oci.millislaw@gmail.com", "oci&my8467");
            //        gmailMsg.To = "millis.office@gmail.com";
            //        gmailMsg.Cc = "ra123@nana.co.il";
            //        gmailMsg.From = "oci.millislaw@gmail.com";
            //        gmailMsg.Subject = "Error in product Client Updater Client id:" + Settings.Default.clientId;
            //        gmailMsg.Body = sb.ToString();
            //        //MailAttachment attachment = new MailAttachment(@"c:\testfile.txt");
            //        //gmailMsg.Attachments.Add(attachment);

            //        gmailMsg.Send();

            //    }
            //    catch (Exception exMail)
            //    {
            //        WriteToLog(exMail, "Error sending mail", false);
            //    }
            //}

        }
        /// <summary>
        /// Opens the log.
        /// </summary>
        /// <param name="type">The type of thew log.</param>
        public static void OpenLog(LogsType type)
        {
            string fileName;
            switch (type)
            {
                case LogsType.Error:
                    fileName = Resources.ErrorLogFileName;
                    break;
                case LogsType.Event:
                    fileName = Resources.EventLogFileName;
                    break;
                default:
                    fileName = "Unkonw";
                    break;
            }
            string logLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), fileName);
            if (File.Exists(logLocation))
                System.Diagnostics.Process.Start("notepad.exe", logLocation);
            else
                MessageBox.Show("Log is empty", Resources.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Writes to event log.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void WriteToEventLog(string message)
        {
            try
            {
                string logLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), Resources.EventLogFileName);
                if (!File.Exists(logLocation))
                    File.WriteAllText(logLocation, message + " |LogTime:" + DateTime.Now.ToString());
                else
                {
                    using (StreamWriter sw = File.AppendText(logLocation))
                    {
                        sw.WriteLine(message + " LogTime:" + DateTime.Now.ToString());
                    }
                }
            }
            catch (Exception exlog)
            {
                WriteToErrorLog(exlog, "Error write to event log", false);
            }
        }

       
    }
}
